package com.prs.crm.action.doc;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.crm.CustomerLevel;
import com.prs.crm.domain.doc.DocLevel;
import com.prs.crm.service.doc.DocLevelService;
import com.prs.crm.service.sys.LogService;

public class DocLevelAction extends BaseAction{
	private DocLevel docLevel;
	private DocLevelService docLevelService;
	private PaginationSupport pagination;
	private Integer[] ids;
	private LogService logService;

	
	@Action("listDocLevel")
	public String listDocLevel() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(DocLevel.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getDocLevelService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	
	@Action("editDocLevel")
	public String editDocLevel() {
		return SUCCESS;
	}
	
	@Action(value="submitDocLevel",results={@Result(name="success",type="redirect",location="listDocLevel"),
			@Result(name="error",type="chain",location="editDocLevel")})
	public String submitDocLevel() {
		if(docLevel.getName() == null || docLevel.getName().trim().equals("")) {
			this.addActionError("请正确填写文档级别");
			return ERROR;
		}if (this.getLogService().getSearchName(DocLevel.class, docLevel
				.getName())) {
			this.addActionError("名称已经存在");
			return ERROR;
		}
		this.getDocLevelService().save(docLevel);
		return SUCCESS;
	}
	
	@Action(value="removeDocLevel",results={@Result(name="success",type="redirect",location="listDocLevel"),
			@Result(name="error",type="chain",location="listDocLevel")})
	public String removeDocLevel() {
		if(ids == null) {
			this.addActionError("请选择要删除的级别");
			return ERROR;
		}
		for(Integer id : ids) {
			DocLevel level = this.getDocLevelService().get(id);
			if(level.getPmDocs().size() != 0) {
				this.addActionError("文档级别已被引用，不能删除");
				return ERROR;
			}
		}
		this.getDocLevelService().batchRemove(ids);
		return SUCCESS;
	}
	
	public DocLevel getDocLevel() {
		return docLevel;
	}
	public void setDocLevel(DocLevel docLevel) {
		this.docLevel = docLevel;
	}
	public DocLevelService getDocLevelService() {
		return docLevelService;
	}
	public void setDocLevelService(DocLevelService docLevelService) {
		this.docLevelService = docLevelService;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	public LogService getLogService() {
		return logService;
	}

	public void setLogService(LogService logService) {
		this.logService = logService;
	}
}
